<template>
  <div class="my-container">
    <!-- 登入 -->
    <div v-if="user" class="heard">
      <div class="box-login">
        <div class="photo-user">
          <van-image round :src="taUrl + userInfo.avatar" />
        </div>
        <div class="username">
          <span>{{ userInfo.nickname }}</span>
        </div>
        <div class="btn">
          <van-button
            class="logout"
            @click="quitBtn"
            size="mini"
            round
            type="primary"
            >退出</van-button
          >
        </div>
        <div class="set">
          <span>编辑个人资料 </span><van-icon name="arrow" />
        </div>
      </div>
    </div>
    <!-- 登入 -->
    <!-- 未登入 -->
    <div v-else class="heard noheard">
      <div class="box-login">
        <div class="photo-user">
          <van-image round src="https://img01.yzcdn.cn/vant/cat.jpeg" />
        </div>
        <div class="username"><span>游客</span></div>
        <div class="btn">
          <van-button
            class="go-login"
            @click="$router.push('/login')"
            size="mini"
            type="primary"
            >去登入</van-button
          >
        </div>
      </div>
    </div>
    <!-- 未登入 -->

    <!-- 中间标签页 -->
    <van-grid clickable class="border-grid" :border="false" :column-num="3">
      <van-grid-item>
        <i class="iconfont icon-coll"></i>
        <div class="text" @click="gofavorate">我的收藏</div>
      </van-grid-item>
      <van-grid-item>
        <i class="iconfont icon-ind"></i>
        <div class="text" @click="gorent">我的出租</div>
      </van-grid-item>
      <van-grid-item>
        <i class="iconfont icon-record"></i>
        <div class="text">看房记录</div>
      </van-grid-item>
      <van-grid-item>
        <i class="iconfont icon-identity"></i>
        <div class="text">成为房主</div>
      </van-grid-item>
      <van-grid-item>
        <i class="iconfont icon-myinfo"></i>
        <div class="text">个人资料</div>
      </van-grid-item>
      <van-grid-item>
        <i class="iconfont icon-cust"></i>
        <div class="text">联系我们</div>
      </van-grid-item>
    </van-grid>
    <!-- 中间标签页 -->
    <!-- 底部页面 -->
    <div class="bottom-img">
      <img src="@/assets/imgs/join.png" alt="" />
    </div>
    <!-- 底部页面 -->
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { TotalUrl } from '@/utils/TotalUrl.js'
import { adminLogout, getUserInfo } from '@/api/user.js'
export default {
  name: 'myContainer',
  components: {},
  props: {},
  data() {
    return {
      taUrl: TotalUrl,
      // 用户信息
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created() {
    this.getUser()
  },
  methods: {
    ...mapMutations(['loginOutSetToken']),
    quitBtn() {
      this.$dialog
        .confirm({
          title: '提示',
          message: '是否确认退出？'
        })
        .then(() => {
          adminLogout()
          // on confirm
          this.loginOutSetToken()
        })
        .catch(() => {
          // on cancel
        })
    },
    // 获取用户信息
    async getUser() {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      const { data: res } = await getUserInfo()
      this.userInfo = res.body
      this.$toast.clear()
    },
    // 跳转收藏列表
    async gofavorate() {
      if (this.user) {
        this.$router.push('/favorate')
      } else {
        try {
          await this.$dialog.confirm({
            title: '好客租房',
            message: '需要登录才能查看,跳转到登录页面'
          })

          this.$router.push('/login')
        } catch {}
      }
    },
    // 跳转出租列表
    async gorent() {
      if (this.user) {
        this.$router.push('/rent')
      } else {
        try {
          await this.$dialog.confirm({
            title: '好客租房',
            message: '需要登录才能查看,跳转到登录页面'
          })
          this.$router.push('/login')
        } catch {}
      }
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  background-color: #fff;
  .heard {
    position: relative;
    height: 750px;
    background: url('@/assets/imgs/avatar.png') no-repeat;
    background-size: contain;
    .box-login {
      position: absolute;
      z-index: 999;
      left: 7.5%;
      width: 85%;
      height: 55%;
      bottom: 0;
      background-color: #fff;
      box-shadow: 0 0 20px 10px #e7e7e7;
      display: flex;
      // justify-content: center;
      align-items: center;
      flex-direction: column;
      .photo-user {
        position: relative;
        background-color: #fff;
        border: 1px solid #9999;
        transform: translateY(-50%);
        box-shadow: 0 4px 4px #bdbdbd;
        display: inline-block;
        border-radius: 50%;
        border: 10px solid #f5f5f5;
        height: 140px;
        width: 140px;
        .van-image {
          width: 100%;
          height: 100%;
        }
      }
      .username {
        position: relative;
        top: -40px;
        font-size: 30px;
      }
      .btn {
        position: relative;
        top: -30px;
        .logout {
          width: 108px;
          height: 40px;
        }
        .go-login {
          margin-top: 30px;
          width: 138px;
          height: 60px;
        }
      }
      .set {
        color: #999;
        font-size: 24px;
      }
    }
  }
  .noheard {
    position: relative;
    height: 750px;
    background: url('@/assets/imgs/bg.png') no-repeat;
    background-size: contain;
  }
  .border-grid {
    /deep/ .van-grid-item {
      margin-bottom: 13px;
      height: 190px;
      .van-grid-item__content {
        padding: 30px 0 !important;
      }
      .iconfont {
        font-size: 40px;
        margin-bottom: 20px;
      }
      .text {
        font-size: 26px;
      }
    }
    // margin-top: 10px;
  }
  .bottom-img {
    text-align: center;
    img {
      width: 92%;
    }
  }
}
</style>
